package com.tsing.cli.common.dto;

import com.alibaba.fastjson2.annotation.JSONField;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 自定义UserDetails对象
 *
 * @author TheTsing
 */
@Getter
@Setter
@Accessors(chain = true)
public class CustomUserDetails implements UserDetails {

    private String id;

    private String username;

    private String password;

    private Set<String> permissions;

    @JSONField(serialize = false)
    private Boolean enabled;

    private String ip;

    private String address;

    private String browser;

    private Long loginTime;

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    @JSONField(serialize = false)
    public String getPassword() {
        return password;
    }

    @Override
    @JSONField(serialize = false)
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return permissions.parallelStream().map(SimpleGrantedAuthority::new).collect(Collectors.toSet());
    }

    @Override
    @JSONField(serialize = false)
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    @JSONField(serialize = false)
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    @JSONField(serialize = false)
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }

}
